﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualDisk
{
    class DirectoryNode:DiskNode
    {
        //节点的儿子列表
        private List<DiskNode> childlist;

        public DirectoryNode(MyString name, DateTime date)
            : base(name, date)
        {
            childlist = new List<DiskNode>();
        }

        public List<DiskNode> Children { get { return childlist; } }

        public override void addNode(DiskNode n)
        {
            if (n == null)
            {
                throw new ArgumentNullException();
            }
            n.Parent = this;
            this.Children.Add(n);
        }

        public override DiskNode getNode(MyString name)
        {
            if (name == new MyString(".."))
            {
                if (Parent != null)
                    return Parent;
                else
                    return this;
            }
            if (name == new MyString("."))
            {
                return this;
            }

            foreach (DiskNode child in childlist)
            {
                if (child.Name.ToLower() == name.ToLower())
                {
                    return child;
                }
            }

            return null;
        }

        public override void removeNode(DiskNode n)
        {
            if (n == null)
            {
                throw new ArgumentNullException();
            }
            n.Parent = null;
            this.Children.Remove(n);
            
        }

        public override void showInfo(ShowOptions option)
        {
            int numOfFile = 0;
            int numOfDirc = 0;
            int sumsize=0;
            Console.WriteLine(this.getPath().ToString() + "的目录");
            foreach (DiskNode node in Children)
            {
                if (option == ShowOptions.DircoryAndFile && node is FileNode)
                {
                    numOfFile++;
                    sumsize += (node as FileNode).Size;
                    Console.WriteLine(node.ToString());
                }
                else if (node is DirectoryNode)
                {
                    numOfDirc++;
                    Console.WriteLine(node.ToString());
                }

            }
               
                Console.WriteLine("        文件 " + numOfFile + " 个      "+sumsize+"字节");
                Console.WriteLine("        目录 " + numOfDirc + " 个");
                Console.WriteLine();
                Console.WriteLine();
            
        }

        public override void showAllInfo(ShowOptions option)
        {
            this.showInfo(option);
            foreach (DiskNode node in Children)
            {
                node.showAllInfo(option);
            }
        }
        public override string ToString()
        {
            String type = "<DIR>" ;
            String size =  "  " ;
            return this.CreateDate.ToString() + "   " + type + "  " + size + "    " + this.Name.ToString();
        }

    }
}
